class GameAction
  
  RESOURCES_REQUIRED_FOR_FARMER = 5
  RESOURCES_REQUIRED_FOR_BARRACK = 100
  RESOURCES_REQUIRED_FOR_WARRIOR = 30
  POPULATION_LIMIT = 200
  FARM_PRODUCE = 6
  MAXIMUM_FARMS = 8
  MAXIMUM_RESOURCES = 50000
  
  def initialize
    @game = Game.instance
  end
  
  def create_farmer name
    sleep 1
    player_stats = @game.get_player_statistics name
    if player_stats.resources > RESOURCES_REQUIRED_FOR_FARMER && player_stats.total_population < POPULATION_LIMIT
      player_stats.farmers+=1  
      player_stats.resources-= RESOURCES_REQUIRED_FOR_FARMER
      player_stats.status = "Creating farmers"
    else
      player_stats.status = "Can't create any more farmers. Not enough resources"
    end
  end
  
  def build_barrack name
    sleep 5
    player_stats = @game.get_player_statistics name
    if player_stats.barracks == 0 && player_stats.resources > RESOURCES_REQUIRED_FOR_BARRACK
      player_stats.barracks+=1
      player_stats.resources-= RESOURCES_REQUIRED_FOR_BARRACK
      player_stats.status = "Building barrack"
      @game.add_to_commentary "#{name} has built a barrack"
    else
      player_stats.status = "Can't create barrack. Not enough resources or maybe you have one already"
    end
  end
  
  def farm name
    sleep 2
    player_stats = @game.get_player_statistics name
    if player_stats.farms < MAXIMUM_FARMS
#      @game.add_resources name, (player_stats.farmers * FARM_PRODUCE)
      player_stats.resources += (player_stats.farmers * FARM_PRODUCE)
      player_stats.resources = MAXIMUM_RESOURCES if player_stats.resources > MAXIMUM_RESOURCES
      player_stats.farms+=1
      player_stats.status = "Farming"
    else
      player_stats.farmers-=1 unless player_stats.farmers == 0
      player_stats.farms-=1 unless player_stats.farms == 0     
      player_stats.status = "Your farmers have started dying now... Create more"
    end
  end
  
  def create_warrior name
    sleep 3
    player_stats = @game.get_player_statistics name
    if player_stats.barracks > 0 && player_stats.resources > RESOURCES_REQUIRED_FOR_WARRIOR && player_stats.total_population < POPULATION_LIMIT
      player_stats.warriors+=1
      player_stats.resources-= RESOURCES_REQUIRED_FOR_WARRIOR
      player_stats.status = "Creating Warriors"
    else
      player_stats.status = "Can't create warriors. Mostly because resources are not enough... BTW have you built a barrack??"
    end    
  end
  
  def attack name, target_player_name
    @game.add_to_commentary "#{name} is attacking #{target_player_name}"
    player_stats = @game.get_player_statistics name
    target_player_stats = @game.get_player_statistics target_player_name
    if target_player_stats.nil?
      player_stats.status = "#{target_player_name} not playing. Is the name correct?"
    end
    
    @attacking_warriors = player_stats.warriors
    @defending_warriors = target_player_stats.warriors
    
    if @attacking_warriors > @defending_warriors
      target_player_stats.warning = "#{name} is attacking you!!! Save yourself if you can..."
      player_stats.status = "Attacking #{target_player_name}"
      sleep 10
      
      @extra_attacking_warriors = @attacking_warriors - @defending_warriors
      attack_equal(@attacking_warriors - @extra_attacking_warriors, name, target_player_name)
      attack_zero(@extra_attacking_warriors, name, target_player_name)
            
    elsif @attacking_warriors == @defending_warriors
      target_player_stats.warning = "#{name} is attacking you!!! Save yourself if you can..."
      player_stats.status = "Attacking #{target_player_name}"
      sleep 10
      
      attack_equal(@attacking_warriors, name, target_player_name)
           
    else
      player_stats.status = "Dude!! You attacked the wrong guy"
      attack target_player_name, name
    end
  end
  
  def gift_warriors name, receiver_name, count
    player_stats = @game.get_player_statistics name
    target_player_stats = @game.get_player_statistics target_player_name
    
    if player_stats.warriors > count 
      player_stats.warriors-=count
      target_player_stats.warriors+=count
    end    
  end

  def gift_resources name, receiver_name, count
    player_stats = @game.get_player_statistics name
    target_player_stats = @game.get_player_statistics target_player_name
    
    if player_stats.resources > count
      player_stats.resources-=count  
      target_player_stats.resources+=count
    end
  end
  
  def attack_equal warriors, name, target_player_name
    player_stats = @game.get_player_statistics name
    target_player_stats = @game.get_player_statistics target_player_name
    
    player_stats.warriors -= warriors*4/10
    target_player_stats.warriors -= warriors*6/10
    
    target_player_stats.farmers -= warriors
    
    target_player_stats.rationalize_damage
  end
  
  def attack_zero warriors, name, target_player_name
    target_player_stats = @game.get_player_statistics target_player_name

    target_player_stats.farmers -= warriors*10
    target_player_stats.resources -= warriors
    
    target_player_stats.rationalize_damage
  end
  
  private :attack_zero, :attack_equal
end
